#include <cassert>
#include "Main/constant.h"
#include "HashtableMsg.h"
#include "Main/qstringhandle.h"

using namespace std;


//----------------------------------------------------------------
//析构函数
HashtableMsg::~HashtableMsg()
{
	clear();
}

//----------------------------------------------------------------
//无参构造函数
HashtableMsg::HashtableMsg()
{

}

//----------------------------------------------------------------
//根据报文字符串进行实始化的构造函有选举权
//输入参数：pStrMsg 需要进行解析的字符串
HashtableMsg::HashtableMsg(QString& sStrMsg)
{
	Parse(sStrMsg);
}

//----------------------------------------------------------------
//函数功能：对报文字符串进行解析
//输入参数：无
//返 回 值：无
void HashtableMsg::clear()
{
	m_vFields.clear();
	m_vRecords.clear();
	m_sStrValue.clear();
}

//----------------------------------------------------------------
//函数功能：对报文字符串进行解析
//输入参数：pStrMsg 需要进行解析的字符串
//返 回 值：如果解析成功返回0，否则返回-1;
int HashtableMsg::Parse(const QString & sStrMsg)
{
    QStringList vFldAndRecs;
    vFldAndRecs = strutils::explode(ApConst::SEPARATOR_RECORD[0],sStrMsg);

	if ( vFldAndRecs.size() == 2 )
	{
        m_vFields = strutils::explode(ApConst::SEPARATOR_RECORD[1], vFldAndRecs.at(0));

        QStringList vRecords;
        vRecords = strutils::explode(ApConst::SEPARATOR_RECORD[1], vFldAndRecs.at(1));

		if ( vRecords.size() > 0 )
		{
			for ( size_t i = 0 ; i < vRecords.size() ; i++ )
			{
                QStringList vRecValue = strutils::explode(ApConst::SEPARATOR_RECORD[2], vRecords.at(i));

				if ( m_vFields.size() <= vRecValue.size() )
				{
                    QMap<QString,QString> mapRec;

					for ( size_t j = 0 ; j < m_vFields.size() ; j++ )
					{						
                        mapRec.insert(m_vFields.at(j),vRecValue.at(j));
					}

					m_vRecords.push_back(mapRec);
				}
				else
				{
                    QMap<QString,QString> mapRec;

					size_t j = 0;
					for (;j < vRecValue.size(); j++ )
					{						
                        mapRec.insert(m_vFields.at(j),vRecValue.at(j));
					}

					for (;j < m_vFields.size() ; j++)
					{
                        mapRec.insert(m_vFields.at(j),"");
					}

					m_vRecords.push_back(mapRec);
				}
			}
		
			m_sStrValue = sStrMsg;
		}
	}

	return 0;
}

//----------------------------------------------------------------
//函数功能：获得集合中的记录数量
//输入参数：无
//返 回 值：记录数
int HashtableMsg::size() const
{
	return m_vRecords.size();
}

QStringList HashtableMsg::GetFields() const
{
	return m_vFields;
}

//----------------------------------------------------------------
//函数功能：将当前对象转换为字符串
//输入参数：v_sOutMsg 用于接收字符串的指针
//返 回 值：无
void HashtableMsg::ToQString(QString& v_sOutMsg) const
{
	v_sOutMsg.clear();

	if ( m_vRecords.size() > 0 )
	{
        QString sKey;
        for (int i = 0 ; i < m_vFields.size(); ++i)
		{
            sKey += m_vFields.at(i);
			sKey += ApConst::SEPARATOR_RECORD[1];
		}
		
		v_sOutMsg += sKey;
		v_sOutMsg += ApConst::SEPARATOR_RECORD[0];

		for ( size_t i = 0 ; i < m_vRecords.size() ; i++ )
		{
			for ( size_t j = 0 ; j < m_vFields.size() ; j++ )
			{
                auto it = m_vRecords.at(i).find(m_vFields.at(j));
                v_sOutMsg += it.value();//data();
				v_sOutMsg += ApConst::SEPARATOR_RECORD[2];
			}
			v_sOutMsg += ApConst::SEPARATOR_RECORD[1];
		}

		v_sOutMsg += ApConst::SEPARATOR_RECORD[0];
	}
}

QString HashtableMsg::ToQString()
{
	m_sStrValue.clear();
    ToQString(m_sStrValue);
	return m_sStrValue;
}

QString HashtableMsg::ToQStringEx() const
{
    QString v_sOutMsg;
    ToQString(v_sOutMsg);
	return v_sOutMsg;
}

//----------------------------------------------------------------
//函数功能：获得指定索引位置的记录
//输入参数：v_iIndex 索引位置
//返 回 值：如果存在，则返回指定索引位置的记录，否则返回0
const QMap<QString,QString>& HashtableMsg::GetMap(size_t v_iIndex)
{
	assert(v_iIndex >= 0 && v_iIndex < m_vRecords.size() );
	return m_vRecords[v_iIndex];
}


//----------------------------------------------------------------
//函数功能：向当前集合中添加值
//输入参数：v_iIndex 需要进行添加的索引位置
//          v_sKey   键名称
//          v_sValue 值
//返 回 值：无
//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,QString v_sValue)
//{
//	assert(v_iIndex >= 0 && v_iIndex <= m_vRecords.size() );
//	if ( v_iIndex == m_vRecords.size() )
//	{
//        map<QString,QString> mapRec;
//        mapRec.insert(map<QString,QString>::value_type(v_sKey,v_sValue));
//		m_vRecords.push_back(mapRec);
//	}
//	else
//	{
//		m_vRecords[v_iIndex][v_sKey]=v_sValue ;
//	}
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,const char* pcVal)
//{
//	assert(v_iIndex >= 0 && v_iIndex <= m_vRecords.size() );
//    QString sValue = pcVal;

//	if ( v_iIndex == m_vRecords.size() )
//	{
//        map<QString,QString> mapRec;
//        mapRec.insert(map<QString,QString>::value_type(v_sKey,sValue));
//		m_vRecords.push_back(mapRec);
//	}
//	else
//	{
//        m_vRecords[v_iIndex].insert(map<QString,QString>::value_type(v_sKey,sValue));
//	}
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,char v_chVal)
//{
//    QString sVal;
//	sVal.append(1,v_chVal);
//	AddValue(v_iIndex,v_sKey,sVal);
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,short v_sVal)
//{
//    AddValue(v_iIndex,v_sKey,strutils::ToQString<short>(v_sVal));
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,int v_iVal)
//{
//    AddValue(v_iIndex,v_sKey,strutils::ToQString<int>(v_iVal));
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,long v_lVal)
//{
//    AddValue(v_iIndex,v_sKey,strutils::ToQString<long>(v_lVal));
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,unsigned char v_uchVal)
//{
//    QString sVal;
//	sVal.append(1,v_uchVal);
//	AddValue(v_iIndex,v_sKey,sVal);
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,unsigned short v_usVal)
//{
//    AddValue(v_iIndex,v_sKey,strutils::ToQString<unsigned short>(v_usVal));
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,unsigned int v_uiVal)
//{
//    AddValue(v_iIndex,v_sKey,strutils::ToQString<unsigned int>(v_uiVal));
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,unsigned long v_ulVal)
//{
//    AddValue(v_iIndex,v_sKey,strutils::ToQString<unsigned long>(v_ulVal));
//}

//void HashtableMsg::AddValue(size_t v_iIndex,QString v_sKey,double v_dlVal)
//{
//    AddValue(v_iIndex,v_sKey,strutils::ToQString<double>(v_dlVal));
//}


//----------------------------------------------------------------
//函数功能：获得指定索引位置记录的值
//输入参数：v_iIndex 需要进行获取的索引位置
//          v_sKey   需要获取值的键名称
//返 回 值：对应索引位置的值
QString   HashtableMsg::GetQString (int v_iIndex,QString v_sKey) const
{
	assert(v_iIndex >= 0 && v_iIndex < m_vRecords.size());

    const QMap<QString,QString>& mapValue = m_vRecords[v_iIndex];
    auto it = mapValue.find(v_sKey);
	if( it != mapValue.end() )
        return it.value();
	else
		return "";

	//return m_vRecords[v_iIndex].find(v_sKey)->second;
}

//char HashtableMsg::GetChar(size_t v_iIndex,QString v_sKey)
//{
//    QString value = GetQString(v_iIndex,v_sKey);
//	if ( value.size() > 0 )
//		return value[0];
//	else
//		return '\0';
//}

//short  HashtableMsg::GetShort  (size_t v_iIndex,QString v_sKey)
//{
//    QString value = GetQString(v_iIndex,v_sKey);
//    return strutils::FromQString<short>(value);
//}

//int HashtableMsg::GetInt(size_t v_iIndex,QString v_sKey) const
//{
//    QString value = GetQString(v_iIndex,v_sKey);

//	// modify by xrs 20140407 解决value为空的时候返回负的最大数的问题
//	if( value.empty() )
//		return 0;
//	else
//        return strutils::FromQString<int>(value);
//}

//long HashtableMsg::GetLong   (size_t v_iIndex,QString v_sKey)
//{
//    QString value = GetQString(v_iIndex,v_sKey);
//    return strutils::FromQString<long>(value);
//}

//unsigned char  HashtableMsg::GetUChar  (size_t v_iIndex,QString v_sKey)
//{
//    QString value = GetQString(v_iIndex,v_sKey);
//	if ( value.size() > 0 )
//		return value[0];
//	else
//		return '\0';
//}

//unsigned short HashtableMsg::GetUShort(size_t v_iIndex,QString v_sKey)
//{
//    QString value = GetQString(v_iIndex,v_sKey);
//	if (value.empty())
//	{
//		return 0;
//	}
//    return strutils::FromQString<unsigned short>(value);
//}

//unsigned int   HashtableMsg::GetUInt   (size_t v_iIndex,QString v_sKey) const
//{
//    QString value = GetQString(v_iIndex,v_sKey);
//	if (value.empty())
//	{
//		return 0;
//	}
//    return strutils::FromQString<unsigned int>(value);
//}

//unsigned long  HashtableMsg::GetULong  (size_t v_iIndex,QString v_sKey)
//{
//    QString value = GetQString(v_iIndex,v_sKey);
//	if (value.empty())
//	{
//		return 0;
//	}
//    return value.toULong();
//}

//double HashtableMsg::GetDouble (size_t v_iIndex,QString v_sKey) const
//{
//    QString value = GetQString(v_iIndex,v_sKey);
//	if (value.empty())
//	{
//		return 0.0;
//	}
//    return strutils::FromQString<double>(value);value.
//}

//----------------------------------------------------------------
//函数功能：插入记录
//输入参数：mRecord 记录指针
//返 回 值：
void HashtableMsg::InsertMap(QMap<QString,QString> mRecord)
{
    QMap<QString,QString> subValueMap;

    QMap<QString, QString>::iterator  iter;

	for(iter = mRecord.begin(); iter != mRecord.end(); iter++)
	{
        subValueMap.insert(iter.key(),iter.value());
	}

	m_vRecords.push_back(subValueMap);
}

void HashtableMsg::SetFields( QStringList v )
{
	m_vFields = v;
}

void HashtableMsg::DelValue(size_t v_iIndex)
{
	if(m_vRecords.size() >= v_iIndex + 1)
	{
		m_vRecords.erase(m_vRecords.begin()+v_iIndex);
	}
}
